package com.cpecc.dao.wuzi;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.User;
import com.cpecc.model.wuzi.WeiXiu;
import com.cpecc.model.wuzi.WuZi;

@Repository
public class WeiXiuDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	public void save(WeiXiu weiXiu){
		Session session=sessionFactory.getCurrentSession();
		session.save(weiXiu);
		session.flush();
	}
	
	public WeiXiu get(String id){
		return (WeiXiu) sessionFactory.getCurrentSession().get(WeiXiu.class, id);
	}

	public WeiXiu getByLiuChengId(String liuChengId){
		String hql="from WeiXiu t where t.liuChengId = ?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, liuChengId);
		List<WeiXiu> list = query.list();
		return list == null ? null : list.get(0);
	}
	
	public boolean cover(WeiXiu weiXiu){
		String hql="update WeiXiu t set t.huanJie=?,t.weiXiuShang=?,t.feiYong=?,t.weiXiuSM=?,t.weiXiuR=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, weiXiu.getHuanJie());
		query.setString(1, weiXiu.getWeiXiuShang());
		query.setDouble(2, weiXiu.getFeiYong());
		query.setString(3, weiXiu.getWeiXiuSM());
		query.setParameter(4, weiXiu.getWeiXiuR());
		query.setString(5, weiXiu.getId());
		return query.executeUpdate()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<WeiXiu> getAllWeiXiuByZT(int zhuangT){
		String hql="from WeiXiu t where t.huanJie.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, zhuangT);
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<WeiXiu> getMyWeiXiu(User user){
		String hql="from WeiXiu t where t.user.id=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<WeiXiu> getMyWeiXiuRW(User user){
		String hql="from WeiXiu t where t.weiXiuR.id=? and t.huanJie.id=403 order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<WeiXiu> getMyWeiXiuQR(User user){
		String hql="from WeiXiu t where t.user.id=? and t.huanJie.id=404 order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, user.getId());
		return query.list();
	}
	
	@SuppressWarnings("unchecked")
	public List<WeiXiu> getAllWeiXiuByWuZi(WuZi wuZi){
		String hql="from WeiXiu t where t.wuZi=? order by t.shenQingRQ desc";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);		
		query.setParameter(0, wuZi);
		return query.list();
	}
	
	public boolean hasWuZi(String wuZiId){
		String hql="from WeiXiu t where t.wuZi.id=? and t.huanJie.id<405";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, wuZiId);
		return query.list().size()>0;
	}
	
	public boolean hasLiuCheng(User user){
		String hql="from WeiXiu t where t.user=? and t.huanJie.id<405";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setParameter(0, user);
		return query.list().size()>0;
	}
	
	@SuppressWarnings("unchecked")
	public List<WeiXiu> activeWeiXiu(){
		String hql="from WeiXiu t where t.huanJie.id<406";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}

	public boolean delete(String id) {
		String hql="delete WeiXiu t where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, id);
		return query.executeUpdate()>0;
	}
}
